package com.yayo.mangement.facade.impl;

import com.yayo.mangement.facade.LoginFacade;
import com.yayo.mangement.info.MenuInfo;
import com.yayo.mangement.info.UserInfo;
import com.yayo.mangement.model.SysMenuDO;
import com.yayo.mangement.model.SysRoleMenuDO;
import com.yayo.mangement.model.SysUserRoleDO;
import com.yayo.mangement.response.Response;
import com.yayo.mangement.service.SysMenuService;
import com.yayo.mangement.service.SysRoleMenuService;
import com.yayo.mangement.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class LoginFacadeImpl implements LoginFacade {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public Response<UserInfo> login(String username, String password) {
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        if(authenticate == null){
            throw new RuntimeException("登陆失败");
        }
        return Response.ok();
    }

    /**
     * 获取该用户所拥有的菜单权限
     * @param userInfo
     */
    private void checkRoleMenu(UserInfo userInfo){
        //根据用户id查询角色id集合
        List<SysUserRoleDO> userRoleDOList = sysUserRoleService.findByUserId(userInfo.getUserId());
        if(!userRoleDOList.isEmpty()){
            List<Long> roleIdList = userRoleDOList.stream().map(SysUserRoleDO::getRoleId).collect(Collectors.toList());
            //根据角色id集合查询菜单权限
            List<SysRoleMenuDO> roleMenuDOList = sysRoleMenuService.findByRoleIdList(roleIdList);
            if(!roleMenuDOList.isEmpty()){
                //根据menuId查询拥有的菜单明细
                List<Long> menuIdList = roleMenuDOList.stream().map(SysRoleMenuDO::getMenuId).collect(Collectors.toList());

                List<SysMenuDO> menuDOList = sysMenuService.findByIds(menuIdList);
                List<MenuInfo> menuInfoList = this.menuDO2Info(menuDOList);
                userInfo.setMenuInfoList(menuInfoList);
            }
        }
    }

    private List<MenuInfo> menuDO2Info(List<SysMenuDO> menuDOList) {
        return menuDOList.stream().map(SysMenu -> {
            MenuInfo menuInfo = new MenuInfo();
            menuInfo.setMenuId(SysMenu.getMenuId());
            menuInfo.setName(SysMenu.getMenuName());
            menuInfo.setUri(SysMenu.getMenuUrl());
            return menuInfo;
        }).collect(Collectors.toList());
    }


}
